package gui;

import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import utility.GooglePoster;
import utility.DisplayUpdateException;
import com.google.gdata.util.ServiceException;

import database.Database;

interface InternetConnectingWindow {
	
	final static String COM_FAILURE = "Unable to sync data with Google records. Available data will be projected.";
	final static String RAISED_EXEP = "Exception was raised";
	final static String EXEP_TITLE = "Error";
	final static String COM_TITLE = "Connection Failure";
	static int STARTUP_TIME = 4;	
}

abstract class StcalWindow implements InternetConnectingWindow{

	private static final long serialVersionUID = 1L;
		
	private static void launch() {
		Database.addMaintenanceCost(STARTUP_TIME);
	}
	
	public static void main(String[] args) throws IOException, ServiceException{
		try{
			launch();
			GooglePoster.post2Google();
		}
		catch(DisplayUpdateException d){
			displayResults(d.getMessage());
		}
		catch(ServiceException s){
			displayResults(COM_FAILURE);
			launch();
		}
		catch (Exception r){
			JOptionPane.showMessageDialog(new JOptionPane(), RAISED_EXEP);
			launch();
		}
	}
	public static void displayResults(final String message){
	    final JDialog dialog = new JDialog(new JFrame(COM_TITLE));
	    final JLabel msg = new JLabel(message);
	    msg.setVisible(true);
	    dialog.add(msg);
	    dialog.setVisible(true); 
	    dialog.pack();
	    dialog.dispose();
	}
}
